home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / FileDialog.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  10.4 KB  |  347 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)FileDialog.java    1.37 98/10/02
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.FileDialogPeer;
  17. import java.io.FilenameFilter;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20.  
  21. /**
  22.  * The <code>FileDialog</code> class displays a dialog window
  23.  * from which the user can select a file.
  24.  * <p>
  25.  * Since it is a modal dialog, when the application calls
  26.  * its <code>show</code> method to display the dialog,
  27.  * it blocks the rest of the application until the user has
  28.  * chosen a file.
  29.  *
  30.  * @see Window#show
  31.  *
  32.  * @version     1.37, 10/02/98
  33.  * @author     Sami Shaio
  34.  * @author     Arthur van Hoff
  35.  * @since       JDK1.0
  36.  */
  37. public class FileDialog extends Dialog {
  38.  
  39.     /**
  40.      * This constant value indicates that the purpose of the file
  41.      * dialog window is to locate a file from which to read.
  42.      */
  43.     public static final int LOAD = 0;
  44.  
  45.     /**
  46.      * This constant value indicates that the purpose of the file
  47.      * dialog window is to locate a file to which to write.
  48.      */
  49.     public static final int SAVE = 1;
  50.  
  51.     /*
  52.      * There are 2 FileDialog Modes : <code>LOAD<code> and
  53.      * <code>SAVE<code>.
  54.      * This integer will represent one or the other.
  55.      * If the mode is not specified it will default to LOAD.
  56.      *
  57.      * @serial
  58.      * @see getMode()
  59.      * @see setMode()
  60.      * @see java.awt.FileDialog#LOAD
  61.      * @see java.awt.FileDialog#SAVE
  62.      */
  63.     int mode;
  64.     /*
  65.      * The string specifying the directory to display
  66.      * in the file dialog.
  67.      * This variable may be null.
  68.      *
  69.      * @serial
  70.      * @see getDirectory()
  71.      * @see setDirectory()
  72.      */
  73.     String dir;
  74.     /*
  75.      * The string specifying the initial value of the
  76.      * filename text field in the file dialog.
  77.      * This variable may be null.
  78.      *
  79.      * @serial
  80.      * @see getFile()
  81.      * @see setFile()
  82.      */
  83.     String file;
  84.     /*
  85.      * The filter used as the file dialog's filename filter.
  86.      * The file dialog will only be displaying files whose
  87.      * names are accepted by this filter.
  88.      * This variable may be null.
  89.      *
  90.      * @serial
  91.      * @see getFilenameFIlter()
  92.      * @see setFilenameFilter()
  93.      * @see FileNameFilter
  94.      */
  95.     FilenameFilter filter;
  96.  
  97.     private static final String base = "filedlg";
  98.     private static int nameCounter = 0;
  99.  
  100.     /*
  101.      * JDK 1.1 serialVersionUID
  102.      */
  103.      private static final long serialVersionUID = 5035145889651310422L;
  104.  
  105.  
  106.     static {
  107.         /* ensure that the necessary native libraries are loaded */
  108.     Toolkit.loadLibraries();
  109.     initIDs();
  110.     }
  111.  
  112.     /**
  113.      * Initialize JNI field and method IDs for fields that may be
  114.        accessed from C.
  115.      */
  116.     private static native void initIDs();
  117.  
  118.     /**
  119.      * Creates a file dialog for loading a file.  The title of the
  120.      * file dialog is initially empty.
  121.      * @param parent the owner of the dialog
  122.      * @since JDK1.1
  123.      */
  124.     public FileDialog(Frame parent) {
  125.     this(parent, "", LOAD);
  126.     }
  127.  
  128.     /**
  129.      * Creates a file dialog window with the specified title for loading
  130.      * a file. The files shown are those in the current directory.
  131.      * @param     parent   the owner of the dialog.
  132.      * @param     title    the title of the dialog.
  133.      */
  134.     public FileDialog(Frame parent, String title) {
  135.     this(parent, title, LOAD);
  136.     }
  137.  
  138.     /**
  139.      * Creates a file dialog window with the specified title for loading
  140.      * or saving a file.
  141.      * <p>
  142.      * If the value of <code>mode</code> is <code>LOAD</code>, then the
  143.      * file dialog is finding a file to read. If the value of
  144.      * <code>mode</code> is <code>SAVE</code>, the file dialog is finding
  145.      * a place to write a file.
  146.      * @param     parent   the owner of the dialog.
  147.      * @param     title   the title of the dialog.
  148.      * @param     mode   the mode of the dialog.
  149.      * @see       java.awt.FileDialog#LOAD
  150.      * @see       java.awt.FileDialog#SAVE
  151.      */
  152.     public FileDialog(Frame parent, String title, int mode) {
  153.     super(parent, title, true);
  154.     this.mode = mode;
  155.     setLayout(null);
  156.     }
  157.  
  158.     /**
  159.      * Construct a name for this component.  Called by getName() when the
  160.      * name is null.
  161.      */
  162.     String constructComponentName() {
  163.         synchronized (getClass()) {
  164.         return base + nameCounter++;
  165.     }
  166.     }
  167.  
  168.     /**
  169.      * Creates the file dialog's peer.  The peer allows us to change the look
  170.      * of the file dialog without changing its functionality.
  171.      */
  172.     public void addNotify() {
  173.         synchronized(getTreeLock()) {
  174.         if (peer == null)
  175.             peer = getToolkit().createFileDialog(this);
  176.         super.addNotify();
  177.     }
  178.     }
  179.  
  180.     /**
  181.      * Indicates whether this file dialog box is for loading from a file
  182.      * or for saving to a file.
  183.      * @return   the mode of this file dialog window, either
  184.      *               <code>FileDialog.LOAD</code> or
  185.      *               <code>FileDialog.SAVE</code>.
  186.      * @see      java.awt.FileDialog#LOAD
  187.      * @see      java.awt.FileDialog#SAVE
  188.      * @see      java.awt.FileDialog#setMode
  189.      */
  190.     public int getMode() {
  191.     return mode;
  192.     }
  193.  
  194.     /**
  195.      * Sets the mode of the file dialog.
  196.      * @param      mode  the mode for this file dialog, either
  197.      *                 <code>FileDialog.LOAD</code> or
  198.      *                 <code>FileDialog.SAVE</code>.
  199.      * @see        java.awt.FileDialog#LOAD
  200.      * @see        java.awt.FileDialog#SAVE
  201.      * @see        java.awt.FileDialog#getMode
  202.      * @exception  IllegalArgumentException if an illegal file
  203.      *                 dialog mode is used.
  204.      * @since      JDK1.1
  205.      */
  206.     public void setMode(int mode) {
  207.     switch (mode) {
  208.       case LOAD:
  209.       case SAVE:
  210.         this.mode = mode;
  211.         break;
  212.       default:
  213.         throw new IllegalArgumentException("illegal file dialog mode");
  214.     }
  215.     }
  216.  
  217.     /**
  218.      * Gets the directory of this file dialog.
  219.      * @return    the (potentially null or invalid) directory of this
  220.      *            FileDialog.
  221.      * @see       java.awt.FileDialog#setDirectory
  222.      */
  223.     public String getDirectory() {
  224.     return dir;
  225.     }
  226.  
  227.     /**
  228.      * Sets the directory of this file dialog window to be the
  229.      * specified directory. Specifying a <code>null</code> or an
  230.      * invalid directory implies an implementation-defined default.
  231.      * This default will not be realized, however, until the user
  232.      * has selected a file. Until this point, <code>getDirectory()</code>
  233.      * will return the value passed into this method.<p>
  234.      * Specifying "" as the directory is exactly equivalent to
  235.      * specifying <code>null</code> as the directory.
  236.      * @param     dir   the specific directory.
  237.      * @see       java.awt.FileDialog#getDirectory
  238.      */
  239.     public void setDirectory(String dir) {
  240.         this.dir = (dir != null && dir.equals("")) ? null : dir;
  241.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  242.     if (peer != null) {
  243.         peer.setDirectory(this.dir);
  244.     }
  245.     }
  246.  
  247.     // NOTE: This method may be called by privileged threads.
  248.     //       This functionality is implemented in a package-private method 
  249.     //       to insure that it cannot be overridden by client subclasses. 
  250.     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  251.     final void setDirectory_NoClientCode(String dir) {
  252.         this.dir = (dir != null && dir.equals("")) ? null : dir;
  253.     }
  254.  
  255.     /**
  256.      * Gets the selected file of this file dialog.
  257.      * @return    the currently selected file of this file dialog window,
  258.      *                or <code>null</code> if none is selected.
  259.      * @see       java.awt.FileDialog#setFile
  260.      */
  261.     public String getFile() {
  262.     return file;
  263.     }
  264.  
  265.     /**
  266.      * Sets the selected file for this file dialog window to be the
  267.      * specified file. This file becomes the default file if it is set
  268.      * before the file dialog window is first shown.<p> Specifying "" as
  269.      * the file is exactly equivalent to specifying <code>null</code>
  270.      * as the file.
  271.      * @param    file   the file being set.
  272.      * @see      java.awt.FileDialog#getFile
  273.      */
  274.     public void setFile(String file) {
  275.         this.file = (file != null && file.equals("")) ? null : file;
  276.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  277.     if (peer != null) {
  278.         peer.setFile(this.file);
  279.     }
  280.     }
  281.  
  282.     // NOTE: This method may be called by privileged threads.
  283.     //       This functionality is implemented in a package-private method 
  284.     //       to insure that it cannot be overridden by client subclasses. 
  285.     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  286.     final void setFile_NoClientCode(String file) {
  287.         this.file = (file != null && file.equals("")) ? null : file;
  288.     }
  289.  
  290.     /**
  291.      * Determines this file dialog's filename filter. A filename filter
  292.      * allows the user to specify which files appear in the file dialog
  293.      * window.
  294.      * @return    this file dialog's filename filter.
  295.      * @see       java.io.FilenameFilter
  296.      * @see       java.awt.FileDialog#setFilenameFilter
  297.      */
  298.     public FilenameFilter getFilenameFilter() {
  299.     return filter;
  300.     }
  301.  
  302.     /**
  303.      * Sets the filename filter for this file dialog window to the
  304.      * specified filter.
  305.      * @param   filter   the specified filter.
  306.      * @see     java.io.FilenameFilter
  307.      * @see     java.awt.FileDialog#getFilenameFilter
  308.      */
  309.     public synchronized void setFilenameFilter(FilenameFilter filter) {
  310.     this.filter = filter;
  311.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  312.     if (peer != null) {
  313.         peer.setFilenameFilter(filter);
  314.     }
  315.     }
  316.  
  317.     private void readObject(ObjectInputStream s)
  318.         throws ClassNotFoundException, IOException
  319.     {
  320.         s.defaultReadObject();
  321.  
  322.     // 1.1 Compatibility: "" is not converted to null in 1.1
  323.     if (dir != null && dir.equals("")) {
  324.         dir = null;
  325.     }
  326.     if (file != null && file.equals("")) {
  327.         file = null;
  328.     }
  329.     }
  330.  
  331.     /**
  332.      * Returns the parameter string representing the state of this file
  333.      * dialog window. This string is useful for debugging.
  334.      * @return  the parameter string of this file dialog window.
  335.      */
  336.     protected String paramString() {
  337.     String str = super.paramString();
  338.     str += ",dir= " + dir;
  339.     str += ",file= " + file;
  340.     return str + ((mode == LOAD) ? ",load" : ",save");
  341.     }
  342.  
  343.     boolean postsOldMouseEvents() {
  344.         return false;
  345.     }
  346. }
  347.